package com.study.zzedu.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.EvaluateItemModel;
import com.study.zzedu.ui.evaluate.EvaluateActivity;
import com.study.zzedu.utils.AppConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EvaluateAdapter extends RecyclerView.Adapter {
    private List<EvaluateItemModel> m;
    EvaluateActivity tActivity;
    List idList;
    private final static int HEAD_COUNT = 1;

    private final static int TYPE_BOTTOM = 1;
    private final static int TYPE_CONTENT = 0;

    public Map<Integer, Float> values = new HashMap<>();

    public EvaluateAdapter(List<EvaluateItemModel> eModel, EvaluateActivity theActivity, List idList) {
        m = eModel;
        tActivity = theActivity;
        this.idList = idList;
        for (int i = 0; i < m.size(); i++) {
            values.put(i, (float) 5.0);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == m.size()) { // 头部
            return TYPE_BOTTOM;
        } else {
            return TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int Viewtype) {
        if (Viewtype == TYPE_CONTENT) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.evaluate_item, parent, false);
            return new ViewHolderContent(view);
        } else {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_lesson_item_bottom, parent, false);
            return new ViewHolderBottom(itemView);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder mholder, @SuppressLint("RecyclerView") final int position) {
        //LogUtil.i("aaaaaa","onBindViewHolder..........."+position+" holder:"+holder.score.getNumStars());
        if (m.size() == 0)
            return;
        if (mholder instanceof ViewHolderContent) {
            ViewHolderContent holder = (ViewHolderContent) mholder;
            EvaluateItemModel em = m.get(position);
            if (!em.getIsFirst())
                holder.tvTitle.setVisibility(View.GONE);
            else
                holder.tvTitle.setVisibility(View.VISIBLE);

            holder.tvTitle.setText(em.getTitle());
            holder.tvTitle1.setText(em.getTitle_1());
            holder.score.setRating((float) values.get(position));

            holder.score.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float v, boolean fromUser) {
                    //此处保持疑问（这里在滚动过程会自动触发，未解。。）
                    if (fromUser) {
                        values.put(position, v);
                    }
                    //LogUtil.i("aaaaaa","ratingBar..........."+v+" position:"+position+" boolean:"+b);
                }
            });
        } else if (mholder instanceof ViewHolderBottom) {
            final ViewHolderBottom vb = (ViewHolderBottom) mholder;
            vb.btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    StringBuffer sb = new StringBuffer();
                    StringBuffer sb2 = new StringBuffer();
                    for (int i = 0; i < values.size(); i++) {
                        sb.append("," + idList.get(i).toString());
                        sb2.append("," + values.get(i).toString());
                    }
                    if (sb.length() < 1)
                        return;
                    if (sb2.length() < 1)
                        return;
                    sb.delete(0, 1);
                    sb2.delete(0, 1);
                    tActivity.submit(tActivity,AppConstant.userCode, AppConstant.courseCode, sb.toString(), sb2.toString(),vb.content.getText().toString());

                }
            });
            return;
        }
    }


    @Override
    public int getItemCount() {
        return m.size() + 1;
    }

    public class ViewHolderContent extends RecyclerView.ViewHolder {
        private TextView tvTitle, tvTitle1;
        private RatingBar score;

        public ViewHolderContent(View view) {
            super(view);
            tvTitle = (TextView) view.findViewById(R.id.tv_title_1);
            tvTitle1 = (TextView) view.findViewById(R.id.tv_title_1_1);
            score = view.findViewById(R.id.ratingBar);
        }
    }

    public class ViewHolderBottom extends RecyclerView.ViewHolder {
        private Button btnSubmit;
        private EditText content;

        public ViewHolderBottom(View view) {
            super(view);
            btnSubmit = view.findViewById(R.id.btn_submit_result);
            content = view.findViewById(R.id.et_evaluate_detail);
        }
    }
}